package com.elitel.frame.base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elitel.frame.base.service.BaseEditconfigService;
import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.base.service.CfgEditService;
import com.elitel.frame.base.service.CfgTableService;
import com.elitel.frame.main.entity.BaseEditconfig;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.CfgEdit;
import com.elitel.frame.main.entity.CfgField;
import com.elitel.frame.main.entity.CfgTable;
import com.elitel.frame.main.entity.ext.BaseEditconfigExt;
import com.elitel.frame.main.entity.ext.CfgEditExt;

/**
 * @description 单表编辑服务
 * @author guoyanfei
 * @date 2018/01/04
 */
@Controller
@RequestMapping("/system/tableEdit")
public class TableEditController {
	private static final Logger logger = LoggerFactory.getLogger(TableEditController.class);
	@Autowired
	private CfgDbService cfgDbService;
	@Autowired
	private CfgTableService cfgTableService;
	@Autowired
	private CfgEditService cfgEditService;
	@Autowired
	private BaseEditconfigService baseEditconfigService;
	
	/**
	 * @description 查询所有数据源
	 * @return CfgDb实体集合
	 * @author guoyanfei
	 * @date 2018/01/04
	 */
	@RequestMapping("/getAllcfgDb")
	@ResponseBody
	public List<CfgDb> getAllcfgDb(HttpServletRequest request){
		List<CfgDb> cfgDbs = new ArrayList<CfgDb>();
		try {
			cfgDbs = cfgDbService.getAllListCfgDb();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return cfgDbs;
	}
	
	/**
	 * @Description 查询当前数据源对应配置的元数据表
	 * @return CfgTable集合
	 * @author guoyanfei
	 * @date 2018/01/04
	 */
	@RequestMapping("/getCfgTableList")
	@ResponseBody
	public List<CfgTable> getCfgTableList(HttpServletRequest request){
		List<CfgTable> cfgTables = new ArrayList<CfgTable>();
		try {
			String dbkey = request.getParameter("dbkey");
			cfgTables = cfgTableService.getListByConnAndtableType(dbkey, 1);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return cfgTables;
		
	}
	
	/**
	 * @description 新增单表编辑服务基本信息
	 * @return 新增结果
	 * @author guoyanfei
	 * @date 2018/01/04
	 */
	@RequestMapping("/addCfgEdit")
	@ResponseBody
	public Integer addCfgEdit(@RequestBody CfgEditExt cfgEditExt){
		Integer result = 0;
		try {
			result = cfgEditService.insertCfgEdit(cfgEditExt);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @Description 修改单表编辑服务基本信息
	 * @return 修改结果
	 * @author guoyanfei
	 * @date 2018/01/04
	 */
	@RequestMapping("/updateCfgEdit")
	@ResponseBody
	public Integer updateCfgEdit(@RequestBody CfgEditExt cfgEditExt){
		Integer result = 0;
		try {
			CfgEdit cfgEdit = new CfgEdit();
			cfgEdit.setGuid(cfgEditExt.getGuid());
			cfgEdit.setServicename(cfgEditExt.getServicename());
			cfgEdit.setDbKey(cfgEdit.getDbKey());
			cfgEdit.setDtConfigId(cfgEditExt.getDtConfigId());
			cfgEdit.setRemark(cfgEditExt.getRemark());
			result = cfgEditService.updateCfgEdit(cfgEdit);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @Description 通过服务ID查询信息
	 * @return cfgEdit实体
	 * @author guoyanfei
	 * @date 2018/01/04
	 */
	@RequestMapping("/selectCfgEidtByguid")
	@ResponseBody
	public CfgEdit selectCfgEidtByguid(HttpServletRequest request){
		CfgEdit cfgEdit = new CfgEdit();
		try {
			String guid = request.getParameter("guid");
			cfgEdit = cfgEditService.selectCfgEditbyguid(guid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return cfgEdit;
	}
	
	/**
	 * @Description 查询服务ID是否存在
	 * @return true(存在)/false(不存在)
	 * @author guoyanfei
	 * @date 2018/01/04
	 */
	@RequestMapping("/isExitInfoByguid")
	@ResponseBody
	public boolean isExitInfoByguid(HttpServletRequest request){
		boolean result = false;
		try {
			String guid = request.getParameter("guid");
			CfgEdit cfgEdit = cfgEditService.selectCfgEditbyguid(guid);
			if(cfgEdit != null){
				result = true;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	/**
	 * @Description 通过服务ID查询字段信息
	 * @return CfgField实体集合
	 * @author guoyanfei
	 * @date 2018/01/04
	 */
	@RequestMapping("/getFieldListByguid")
	@ResponseBody
	public List<CfgField> getFieldListByguid(HttpServletRequest request){
		List<CfgField> cfgFields = new ArrayList<CfgField>();
		try {
			String guid = request.getParameter("guid");
			cfgFields = cfgEditService.selectFieldByguid(guid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return cfgFields;
	}
	
	/**
	 * @Description 批量新增编辑信息配置
	 * @return 新增条数
	 * @author guoyanfei
	 * @date 2018/01/04
	 */
	@RequestMapping("/addBaseEditconfig")
	@ResponseBody
	public Integer addBaseEditconfig(HttpServletRequest request){
		Integer result = 0;
		try {
			String serviceId = request.getParameter("serviceId");
			String fieldIds = request.getParameter("fieldIds");
			result = baseEditconfigService.insertBaseEditconfig(serviceId, fieldIds);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}

	@RequestMapping("/batchAddBaseEditconfigList")
	@ResponseBody
	public Integer batchAddBaseEditconfigList(HttpServletRequest request, @RequestBody List<BaseEditconfig> baseEditConfigList){
		Integer result = 0;
		try {
			String serviceId = request.getParameter("serviceId");
			result = baseEditconfigService.batchAddBaseEditconfigList(serviceId, baseEditConfigList);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}

	
	/**
	 * @Description 通过服务ID查询编辑信息配置
	 * @return BaseEditconfigExt实体List集合
	 * @author guoyanfei
	 * @date 2018/01/05
	 */
	@RequestMapping("/getAllBaseEditByServiceId")
	@ResponseBody
	public List<BaseEditconfigExt> getAllBaseEditByServiceId(HttpServletRequest request){
		List<BaseEditconfigExt> baseEditconfigExts = new ArrayList<BaseEditconfigExt>();
		try {
			String serviceId = request.getParameter("serviceId");
			baseEditconfigExts = baseEditconfigService.getAllBaseEditByServiceId(serviceId);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return baseEditconfigExts;
	}
	
	/**
	 * @Description 通过guid批量删除信息
	 * @return 删除结果
	 * @author guoyanfei
	 * @date 2018/01/05
	 */
	@RequestMapping("/delBaseEditByguid")
	@ResponseBody
	public Integer delBaseEditByguid(HttpServletRequest request){
		Integer result = 0;
		try {
			String guids = request.getParameter("guids");
			result = baseEditconfigService.deleteBaseEditByguid(guids);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	/**
	 * @Description 修改编辑信息配置
	 * @return 修改结果
	 * @author guoyanfei
	 * @date 2018/01/05
	 */
	@RequestMapping("/updateBaseEditByguid")
	@ResponseBody
	public Integer updateBaseEditByguid(@RequestBody BaseEditconfig baseEditconfig){
		Integer result = 0;
		try {
			result = baseEditconfigService.updateBaseEdit(baseEditconfig);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}

	@RequestMapping("/batchUpdateBaseEditconfigList")
	@ResponseBody
	public Integer batchUpdateBaseEditconfigList(@RequestBody List<BaseEditconfig> baseEditconfig){
		Integer result = 0;
		try {
			result = baseEditconfigService.batchUpdateBaseEditList(baseEditconfig);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}


}
